package com.adcc.loadsheet.monitor.controller;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.monitor.entity.ServerLog;
import com.adcc.loadsheet.monitor.service.ServerLogService;
import com.adcc.loadsheet.monitor.util.Constant;
import com.adcc.loadsheet.monitor.vo.ResultStatus;
import com.adcc.loadsheet.monitor.vo.ResultVO;
import com.adcc.loadsheet.monitor.vo.ServerLogVO;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 系统日志控制层
 */
@Controller
@RequestMapping(value = "/log", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class LogController {

    @Autowired
    private ServerLogService serverLogService;

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO find(String serverName, String type, String content, String startTime, String endTime, int currentPage, int pageSize){
        ResultVO vo = new ResultVO();
        try {
            PageInfo<ServerLog> pageInfo = serverLogService.find(serverName, type, content, startTime, endTime, currentPage, pageSize);
            if (pageInfo.getList() != null && pageInfo.getList().size() > 0) {
                List<ServerLogVO> voList = Lists.newArrayListWithExpectedSize(pageInfo.getList().size());
                for(ServerLog serverLog : pageInfo.getList()){
                    voList.add(new ServerLogVO(serverLog));
                }
                vo.getData().put("logs", voList);
            }
            vo.getData().put("pageInfo", pageInfo);
        } catch (Exception ex) {
            Log.error(LogController.class, "find() error", ex);
            vo.setStatus(ResultStatus.FAILED);
            vo.setErrorReason(String.format(Constant.ERROR_QUERY, "系统日志"));
        }
        return vo;
    }
}
